package com.hdl;

import java.util.HashMap;
import java.util.Map;



/**
 * <pre>
 * R - Result Response
 * 通用结果返回类
 * ClassName : com.teach.R<T>
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-15 07:29:28
 * Version : 1.0
 * Copyright 2020 jlzhou.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 * @param <T>
 **/
public class R<T> {
	
	/**
	 * 默认错误编号:500
	 */
	public final static int ERROR_CODE = 500;
	/**
	 * 默认成功编号：200
	 */
	public final static int OK_CODE = 200;
	
	/**
	 * 默认错误提示:操作失败
	 */
	public final static String ERROR_MESSAGE = "操作失败";
	
	/**
	 * 默认成功提示：操作成功
	 */
	public final static String OK_MESSAGE = "操作成功";

	private Integer code;
	private String message;
	private T data;
	
	
	public static <T> R<T> ok(String message,T data){
		return new R<T>(OK_CODE,message,data);
	}
	
	public static R<Object> ok(String message){
		return new R<Object>(OK_CODE,message,null);
	}
	
	public static <T> R<T> ok(T data){
		return new R<T>(OK_CODE,OK_MESSAGE,data);
	}
	
	public static R<Object> ok(){
		return new R<Object>(OK_CODE,OK_MESSAGE,null);
	}
	
	public static R<Object> error(String message){
		return new R<Object>(ERROR_CODE,message,null);
	}
	
	public static <T> R<T> error(String message,T data){
		return new R<T>(ERROR_CODE,message,data);
	}
	
	public static <T> R<T> error(T data){
		return new R<T>(ERROR_CODE,OK_MESSAGE,data);
	}
	
	public static R<Object> error(){
		return new R<Object>(ERROR_CODE,OK_MESSAGE,null);
	}
	
	public static <T> R<T> msg(Integer code,String message,T data){
		return new R<T>(code,message,data);
	}
	
	public static R<Object> msg(Integer code,String message){
		return new R<Object>(code,message,null);
	}
	
	public static  R<Map<String, Object>> map(Integer code,String message){
		return new R<Map<String, Object>>(code,message,new HashMap<String, Object>());
	}
	
	public R() {
		super();
	}
	public R(Integer code, String message, T data) {
		super();
		this.code = code;
		this.message = message;
		this.data = data;
	}
	public Integer getCode() {
		return code;
	}
	public R<T> setCode(Integer code) {
		this.code = code;
		return this;
	}
	public String getMessage() {
		return message;
	}
	public R<T> setMessage(String message) {
		this.message = message;
		return this;
	}
	public T getData() {
		return data;
	}
	public R<T> setData(T data) {
		this.data = data;
		return this;
	}
	
	/**
	 * - 属性date是Map才可以执行的方法
	 * @param key
	 * @param value
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public R<T> push(String key,Object value){
		if(this.getData() instanceof Map) {
			((Map<String, Object>)this.getData()).put(key, value);
			return this;
		}else {
			throw new RuntimeException("R.data not is Map");
		}
	}

}
